package org.feng.convert;

import org.feng.api.animal.constant.AnimalSexEnum;
import org.feng.api.animal.constant.AnimalTypeEnum;
import org.feng.api.animal.request.AddAndUpdateAnimalRequest;
import org.feng.api.animal.response.QueryAnimalVo;
import org.feng.entity.Animal;
import org.feng.util.TimeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 动物实例转换
 *
 * @author feng
 */
public class AnimalConvert {

    public static Animal convertToAnimal(AddAndUpdateAnimalRequest request) {
        Animal animal = new Animal();
        BeanUtils.copyProperties(request, animal);
        animal.setAnimalType(AnimalTypeEnum.getByCode(request.getAnimalTypeCode()));
        animal.setAnimalSex(AnimalSexEnum.getByCode(request.getAnimalSexCode()));
        return animal;
    }

    public static List<QueryAnimalVo> convertToQueryVos(List<Animal> records) {
        if (CollectionUtils.isEmpty(records)) {
            return Collections.emptyList();
        }

        return records.stream().map(AnimalConvert::convertToQueryVo).collect(Collectors.toList());
    }

    public static QueryAnimalVo convertToQueryVo(Animal animal) {
        QueryAnimalVo queryAnimalVo = new QueryAnimalVo();
        BeanUtils.copyProperties(animal, queryAnimalVo);

        // 动物类型
        AnimalTypeEnum animalType = animal.getAnimalType();
        if (animalType != null) {
            queryAnimalVo.setAnimalTypeCode(animalType.getCode());
            queryAnimalVo.setAnimalTypeName(animalType.getDesc());
        }

        // 动物性别
        AnimalSexEnum animalSex = animal.getAnimalSex();
        if (animalSex != null) {
            queryAnimalVo.setAnimalSexCode(animalSex.getCode());
            queryAnimalVo.setAnimalSexName(animalSex.getDesc());
        }

        // 创建时间
        queryAnimalVo.setCreateTimeStr(TimeUtil.parse(queryAnimalVo.getCreateTime()));
        // 更新时间
        queryAnimalVo.setUpdateTimeStr(TimeUtil.parse(queryAnimalVo.getUpdateTime()));
        return queryAnimalVo;
    }
}
